package com.wdc.binarytree;

/**
 * leetcode 1008 根据BST的前序遍历，构造一棵二叉树
 */
public class E07Leetcode1008 {
    public TreeNode bstFromPreorder(int[] preorder) {
        if (preorder.length == 0) {
            return null;
        }

        TreeNode root = new TreeNode(preorder[0]);
        for (int i = 1; i < preorder.length; i++) {
            int val = preorder[i];
            insert(root,val);

        }

        return root;
    }

    private  TreeNode insert(TreeNode node, int val) {
        if (node == null){
            return new TreeNode(val);
        }
        if (val < node.val){
            node.left = insert(node.left,val);
        }
        if (val > node.val){
            node.right = insert(node.right,val);
        }


        return node;
    }

    public static void s(){

    }







}
